package com.woniu.car.controller;

import com.woniu.car.common.BeanCopyUtils;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.domain.TExamine;
import com.woniu.car.domain.TShops;
import com.woniu.car.dto.ExamineInfoDto;
import com.woniu.car.dto.ExamineResultDto;
import com.woniu.car.dto.ExamineUpdateDto;
import com.woniu.car.exception.ParamException;
import com.woniu.car.param.QueryExamineResultParam;
import com.woniu.car.param.QueryExaminesParam;
import com.woniu.car.param.UpdateExamineParam;
import com.woniu.car.service.TExamineService;
import com.woniu.car.service.TShopService;
import com.woniu.car.util.AccountResult;
import com.woniu.car.util.AccountStatus;
import com.woniu.car.util.Code;
import com.woniu.car.util.IsExamine;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WWX
 * @since 2021-06-19
 */
@Api(tags = "数据审核控制器")
@RestController
@RequestMapping("/tExamine")
public class TExamineController {
    @Resource
    private TExamineService tExamineService;
    @Resource
    private TShopService tShopService;
    /**
     *@Author 王维鑫
     *@Description 后台查看待审核数据
     *@Param
     *@return
     **/
    @ApiOperation("后台查看待审核数据接口")
    @GetMapping("showExamines")
    public ResponseEntity showExamines() throws ParamException {
        TExamine tExamine = new TExamine();
        tExamine.setExamineStatus(AccountStatus.FORZEN);
        //tExamine.setBackstageId(queryExaminesParam.getBackstageId());
        List<TExamine> tExamines = tExamineService.queryExamine(tExamine);
        if(tExamines.size()==0){
            throw new ParamException("暂时没有待审核数据");
        }
        List<ExamineInfoDto> ts =  BeanCopyUtils.copyList(tExamines,ExamineInfoDto::new);
        return ResponseEntity.builderList(ExamineInfoDto.class).setObject(ts).setCode(Code.NORMAL).setMsg("获取到待审核数据");
    }
    /**
     *@Author 王维鑫
     *@Description 提交审核结果
     *@Param
     *@return
     **/
    @ApiOperation("审核数据提交接口")
    @PostMapping("updateExamines")
    public ResponseEntity updateExamines(UpdateExamineParam updateExamineParam) throws Exception {
        long time = new Date().getTime();
        TExamine tExamine = new TExamine();
        BeanCopyUtils.copyProperties(updateExamineParam,tExamine);
        tExamine.setExamineTime(time);
        //更改审核信息状太
        tExamine.setExamineStatus(IsExamine.TRUE);
        //如果审核结果为通过，则取更改商铺状态为运营
        if(updateExamineParam.getExamineResult()== AccountResult.PASS){
            Integer examineId = updateExamineParam.getExamineId();
            TExamine tExamine1 = new TExamine();
            tExamine1.setExamineId(examineId);
            List<TExamine> tExamines = tExamineService.queryExamine(tExamine1);
            Integer shopId = tExamines.get(0).getShopId();
            TShops tShops = new TShops();
            tShops.setShopId(shopId);
            tShops.setShopStatus(AccountStatus.NORMAL);
            tShopService.updateShop(tShops);
        }
        ExamineUpdateDto examineUpdateDto = tExamineService.updateExamine(tExamine);
        return ResponseEntity.builder(ExamineUpdateDto.class).setObject(examineUpdateDto).setCode(Code.NORMAL).setMsg("审核成功");
    }


    /**
     *@Author 王维鑫
     *@Description 查看审核结果
     *@Param
     *@return
     **/
    @ApiOperation("查看审核结果")
    @GetMapping("queryExamineResult")
    public ResponseEntity queryExamineResult(QueryExamineResultParam param) throws ParamException {
        ExamineResultDto examineResultDto = tExamineService.queryExamineResult(param);
        return ResponseEntity.builder(ExamineResultDto.class).setObject(examineResultDto).setMsg("查到审核结果").setCode(Code.NORMAL);
    }

}

